﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using System.Data.SqlClient;
using System.Data;



namespace SQLServerDAL
{
    public class UserDal : IUserDAL
    {
        public int AddUser(Model.Users user)
        {
            string strSql = "Insert into Users Values(@username,@password,@isadmin,@containfields)";
            SqlParameter[] arr = new SqlParameter[]
            {
                new SqlParameter( "@username",user.UserName),
                new SqlParameter( "@password",user.Password),
                new SqlParameter( "@isadmin",user.IsAdmin),
                new SqlParameter( "@containfields",user.ContainFileds)
            
            };
            return SQLHelper.ExecuteSql(strSql, arr);
        }

        public int DeleteUser(string userId)
        {
            string strSql = "Delete Users where UserID = @userid";
            SqlParameter[] para = new SqlParameter[]
            {
               new SqlParameter ("@userid", userId)
            };
            return SQLHelper.ExecuteSql(strSql, para);
        }

        public int UpdateUser(Model.Users user)
        {
            string strSql = "update Users set UserName=@username,Password=@password,ContainsProFileds=@containsfileds where UserID = @userid";
            SqlParameter[] para = new SqlParameter[]
            {
               new SqlParameter ("@username", user.UserName),
               new SqlParameter ("@password", user.Password),
               new SqlParameter ("@containsfileds", user.ContainFileds),
               new SqlParameter ("@userid", user.Id)
            };
            return SQLHelper.ExecuteSql(strSql, para);
        }


        public DataTable GetAllUser()
        {
            string strSql = "select * from Users";
            return SQLHelper.ExecuteDt(strSql);
        }


        public DataTable GetUser(string username, string password)
        {
            string strSql = "select * from Users where UserName=@username and Password=@password";
            SqlParameter[] para = new SqlParameter[]
            {
               new SqlParameter ("@username", username),
               new SqlParameter ("@password", password)
              
            };
            return SQLHelper.ExecuteDt(strSql, para);
        }

        public int GetUser(string username)
        {
            string strSql = "select * from Users where UserName=@username";
            SqlParameter[] para = new SqlParameter[]
            {
               new SqlParameter ("@username", username)      
            };
            return SQLHelper.ExecuteDt(strSql, para).Rows.Count;
        }
        public DataTable GetUserByName(string username)
        {
            string strSql = "select * from Users where UserName=@username";
            SqlParameter[] para = new SqlParameter[]
            {
               new SqlParameter ("@username", username)      
            };
            return SQLHelper.ExecuteDt(strSql, para);
        }
    }
}
